<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="老师名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生微信" prop="wecat">
        <el-input
          v-model="queryParams.wecat"
          placeholder="请输入学生微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生电话" prop="stuPhone">
        <el-input
          v-model="queryParams.stuPhone"
          placeholder="请输入学生电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="appointmentTime">
        <el-date-picker clearable
          v-model="queryParams.appointmentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="问题类型" prop="problemType">
        <el-select v-model="queryParams.problemType" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in dict.type.problem_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.help_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['education:assist:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['education:assist:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['education:assist:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['education:assist:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="帮扶老师" align="center" prop="teacherName" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="学生微信" align="center" prop="wecat" />
      <el-table-column label="学生电话" align="center" prop="stuPhone" />
      <el-table-column label="所在班级" align="center" prop="className" />
      <el-table-column label="预约时间" align="center" prop="appointmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="problemType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.problem_type" :value="scope.row.problemType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.help_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="intro" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleComplete(scope.row)"
            v-hasPermi="['education:assist:edit']"
          >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['education:assist:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['education:assist:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-right"
            @click="handleTransfer(scope.row)"
            v-hasPermi="['education:assist:remove']"
          >移交</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改帮扶管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="帮扶老师" prop="teacherName" >
          <el-input v-model="form.teacherName" placeholder="请输入老师名" :disabled="true"/>
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生名" :disabled="true"/>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="form.status">
            <el-option
              v-for="dict in dict.type.help_status"
              :key="dict.value"
              :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="完成时间" prop="completionTime">
          <el-date-picker clearable
                          v-model="form.completionTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 移交帮扶对话框 -->
    <el-dialog :title="title" :visible.sync="transferOpen" width="500px" append-to-body custom-class="el-dialog-s">
      <el-form ref="transferForm" :model="transferForm" :rules="transferFormRules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName" >
          <el-input v-model="transferForm.studentName" placeholder="请输入学生名" :disabled="true" />
        </el-form-item>
        <el-form-item label="原始老师" prop="originalTeacher" >
          <el-input v-model="transferForm.originalTeacher" placeholder="请输入原始老师" :disabled="true" />
        </el-form-item>
        <el-form-item label="移交老师" prop="transferTeacher">
          <el-select v-model="transferForm.transferTeacher" placeholder="移交老师">
            <el-option
              v-for="item in teachers"
              :label="item.teacherName"
              :value="item.teacherName">
              <span style="float: left">{{ item.teacherName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师备注" prop="remark">
          <el-input v-model="transferForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTransferForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssist, getAssist, delAssist, addAssist, updateAssist } from "@/api/education/assist";
import {listTeacher} from "@/api/education/teacher";
import {addTransfer} from "@/api/education/transfer";

export default {
  name: "Assist",
  dicts: ['problem_type', 'help_status'],
  data() {
    return {
      teachers:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮扶管理表格数据
      assistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      transferOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        studentName: null,
        wecat: null,
        stuPhone: null,
        appointmentTime: null,
        completionTime: null,
        problemType: null,
        status: null,
        intro: null
      },
      // 表单参数
      form: {},
      // 移交帮扶表单
      transferForm: {},
      // 表单校验
      rules: {
      },
      transferFormRules:{
        teacherName: [
          { required: true, trigger: "blur", message: "请选择移交老师" },
        ],
        remark: [
          { required: true, trigger: "blur", message: "备注不能为空" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTeachers();
  },
  methods: {
    getTeachers(){
      listTeacher().then(res =>{
        this.teachers = res.rows;
      })
    },
    /** 查询帮扶管理列表 */
    getList() {
      this.loading = true;
      listAssist(this.queryParams).then(response => {
        this.assistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.transferOpen = false;
      this.transferReset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        studentName: null,
        wecat: null,
        stuPhone: null,
        appointmentTime: null,
        completionTime: null,
        problemType: null,
        status: 0,
        intro: null
      };
      this.resetForm("form");
    },

    // 移交帮扶表单重置
    transferReset() {
      this.transferForm = {
        id: null,
        transferTeacher: null,
        originalTeacher: null,
        studentName: null,
        remark: null,
      };
      this.resetForm("transferForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加帮扶管理";
    },
    /** 任务完成按钮 */
    handleComplete(row) {
      const id = row.id || this.ids
      getAssist(id).then(response => {
        this.form = response.data;
        if (row.id != null) {
          this.form.status = 1;
          updateAssist(this.form).then(response => {
            this.$modal.msgSuccess("帮扶已完成");
            this.getList();
          });
        }
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssist(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改帮扶管理";
      });
    },
    /** 移交帮扶操作 */
    handleTransfer(row) {
      this.transferReset();
      const id = row.id || this.ids
      getAssist(id).then(response => {
        console.log(response.data.studentName);
        this.form = response.data;
        this.transferForm.studentName = response.data.studentName;
        this.transferForm.originalTeacher = response.data.teacherName;
        this.transferOpen = true;
        this.title = "移交帮扶";
      });
    },
    /** 提交移交帮扶信息  */
    submitTransferForm(){
      this.$refs["transferForm"].validate(valid => {
        if (valid) {
            this.form.teacherName = this.transferForm.transferTeacher;
            this.form.status = 3;
            updateAssist(this.form).then(response => {
              this.$modal.msgSuccess("移交老师成功");
              this.getList();
            });
            addTransfer(this.transferForm).then(response => {
              this.$modal.msgSuccess("移交历史保存成功");
              this.transferOpen = false;
              this.getList();
            });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除帮扶管理编号为"' + ids + '"的数据项？').then(function() {
        return delAssist(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('education/assist/export', {
        ...this.queryParams
      }, `assist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
